package top.lyjwn.ajui.widget.image;

import android.content.Context;
import android.os.Environment;

import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.cache.ExternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.ExternalPreferredCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;

import java.io.InputStream;

/**
 * Glide缓存
 *
 * @author luoyujie
 */
public abstract class AJGlideModule extends AppGlideModule {

    protected abstract String path();

    // 1024 * 1024 * 20;
    protected abstract int memoryCacheSize();

    // 1024 * 1024 * 128;
    protected abstract int diskCacheSize();

    @Override
    public void applyOptions(final Context context, GlideBuilder builder) {
        //获取内存的默认配置
//        MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context).build();
//        int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
//        int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
//        int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
//        int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
//        builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
//        builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));

        //内存缓存相关,默认是24m
        builder.setMemoryCache(new LruResourceCache(memoryCacheSize()));
        //设置磁盘缓存及其路径
        //
        builder.setDiskCache(new ExternalPreferredCacheDiskCacheFactory(context, path(), diskCacheSize()));
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            builder.setDiskCache(new DiskLruCacheFactory(path(), diskCacheSize()));
        } else {
            builder.setDiskCache(new ExternalPreferredCacheDiskCacheFactory(context, path(), diskCacheSize()));
        }
    }


    @Override
    public boolean isManifestParsingEnabled() {
        //不使用清单配置的方式,减少初始化时间
        return false;
    }
}
